#include "MainHeader.h"

cOAMEdit::cOAMEdit(){
}
cOAMEdit::~cOAMEdit(){
}
BOOL CALLBACK	OAMProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	
	
	
	switch (message) 
	{
	case WM_INITDIALOG:
		OamEditor.me = hWnd;
		OamEditor.Create();
		
		break;
		
	case WM_COMMAND:
		
		switch(LOWORD(wParam)){
        case lstSprites:
			if(HIWORD(wParam)==CBN_SELCHANGE){
                OamEditor.GetSpriteData();
				SendMessage(hWnd,WM_COMMAND,cmdGTO,0);
			}
			break;
		case cmdSG:
            SaveGFX();
			break;
		case mnuSP:
            SavePal();
			break;
		case mnuIG:
            ImportGFX();
			break;
		case mnuEP:
			ExportPal();
			break;
		case mnuIP:
			ImportPal();

			break;
		case mnuEG:
			ExportGFX();
		break;
		case cmdGTO:
            OamEditor.GetFrames();
			OamEditor.SetFrames();
			OamEditor.SetupPreview();
        OamEditor.SetParts();
			break;
        case cboParts:
			if(HIWORD(wParam)==CBN_SELCHANGE){
			 OamEditor.SetParts();
			}
		case cboBPal:
			if(HIWORD(wParam)==CBN_SELCHANGE){
              OamEditor.UpdateSprite();
			}
			break;

		case chkHorz:
             OamEditor.UpdateSprite();
			break;
        case chkVert:
             OamEditor.UpdateSprite();
			break;
		case cboShape:
			if(HIWORD(wParam)==CBN_SELCHANGE){
              OamEditor.UpdateSprite();
			}
			break;
		case cboSize:
			if(HIWORD(wParam)==CBN_SELCHANGE){
              OamEditor.UpdateSprite();
			}
			break;
		case WM_DESTROY:
			ShowWindow(hWnd, SW_HIDE);
			break;

		}
		
		break; 
		
	}
	
	return 0;
}

int cOAMEdit::Create(){
    int i=0; 
	char sillystring[16]={0};
	//Init all the combos
	 cboFrames.Init(GetDlgItem(me,cboFI));
     cboPartNo.Init(GetDlgItem(me,cboParts));
     cboShapes.Init(GetDlgItem(me,cboShape));
     cboSizes.Init(GetDlgItem(me,cboSize));//Changes based on cboShape
     lstSprite.Init(GetDlgItem(me,lstSprites));
	 VChk.SetCnt(GetDlgItem(me,chkVert));
     HChk.SetCnt(GetDlgItem(me,chkHorz));
     cboShapes.Additem("Square");   
	 cboShapes.Additem("Horizontal");  
	 cboShapes.Additem("Vertical");
	 cboShapes.SetListIndex(0);
	 cboPal.Init(GetDlgItem(me,cboBPal));
	 UpdateSize();
     memset(OamEditor.SGBSpr.PreviewPal,0,0x800);
	 Sprite.Create(512,512);
	 Tiles .Create(512,512);
     cboFrames.Clear();
	 for(i=0;i<0xFF;i++){

             
		 sprintf(sillystring,"%X",i);
		 if(i<16)cboPal.Additem(sillystring);
		 lstSprite.Additem(sillystring);
	 }
	 CreatePalHWND();
     CreateTileHWND();
     CreateSpriteHWND();

	return 0;
}

int cOAMEdit::CreatePalHWND(){

WNDCLASSEX blahf;
	memset(&blahf,0,sizeof(blahf));
	blahf.cbSize = sizeof(blahf);		   // size of structure 
	blahf.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	blahf.lpfnWndProc = &OAMPalProc;	  // points to window procedure 
	blahf.cbClsExtra = NULL;				// no extra class memory 
	blahf.cbWndExtra =NULL; 			   // no extra window memory 
	blahf.hInstance = hGlobal;		   // handle to instance 
	blahf.hIcon = NULL; 			 // predefined app. icon 
	blahf.hCursor = NULL;				// predefined arrow 
	blahf.hbrBackground = (HBRUSH)GetStockObject(15);				   // white background brush 
	blahf.lpszMenuName =  NULL;    // name of menu resource 
	blahf.lpszClassName = "cOAMPAL";  // name of window class 
	
	
	// Register the window class. 
	RegisterClassEx(&blahf);
	
	
	//Create the Window
	
	
	
	hwndPal= CreateWindowEx(NULL,"cOAMPAL",NULL,WS_VISIBLE| WS_CHILD,5,12,256,256,GetDlgItem(me,fraPal),0,hGlobal,0);



	return 0;
}


LRESULT CALLBACK OAMPalProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
    int buf=0;
	int i=0;
	int j=0;
	switch (message) 
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		DrawPal(hdc,OamEditor.SGBSpr.PreviewPal);
		EndPaint(hWnd, &ps);
		break;
		
	case WM_RBUTTONDOWN:
		buf=(GetX(lParam)/16)+((GetY(lParam)/16)*16);
        sprintf(procstring,"Physical Pal Number:%d, Pal Number: %d\nCurrent Pal Slot: %d\nCurrent Pal Number:%d\nCurrent Palette Value is %X",(buf)+128,(buf),(buf%16)+1,SpriteSet.pal[buf+128]);
		i=LoadInput(OamEditor.SGBSpr.PreviewPal[buf]);
		if(i!=-1){
	
		//for(j=0;j<512;j++)OamEditor.SGBSpr.PreviewPal[i]=0;
		OamEditor.SGBSpr.PreviewPal[buf+128]=RGB((i /0x10000),((i&0xFF00)/0x100),(i&0xFF));//(long)(i&0xFFFFFF);
		InvalidateRect(SpriteSet.PalView,0,1);
		OamEditor.Tiles.SetPalette(OamEditor.SGBSpr.PreviewPal);
		OamEditor.SGBSpr.PreviewSprite.SetPalette(SpriteSet.pal);
	
		for(i= 512; i<1024;i++){
		  OamEditor.Tiles.Draw(OamEditor.SGBSpr.Tiles,((i-512) % 32)*8,((i-512)/32)*8,0x8000+i);
		}
		
        //SendMessage(SpriteSet.me,WM_COMMAND,0x104a7,0);
		DrawPSprite(&OamEditor.SGBSpr);
		InvalidateRect(OamEditor.hwndSpritePreview,0,1);    
		InvalidateRect(OamEditor.hwndTiles,0,1);
		InvalidateRect(hWnd,0,1);
		//InvalidateRect(SpriteSet.SprView,0,1);
		}
		break; 
		
		
		
		
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
	//return 0;
}
int cOAMEdit::CreateTileHWND(){
WNDCLASSEX blahf;
	memset(&blahf,0,sizeof(blahf));
	blahf.cbSize = sizeof(blahf);		   // size of structure 
	blahf.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	blahf.lpfnWndProc = &OAMTileProc;	  // points to window procedure 
	blahf.cbClsExtra = NULL;				// no extra class memory 
	blahf.cbWndExtra =NULL; 			   // no extra window memory 
	blahf.hInstance = hGlobal;		   // handle to instance 
	blahf.hIcon = NULL; 			 // predefined app. icon 
	blahf.hCursor = NULL;				// predefined arrow 
	blahf.hbrBackground = (HBRUSH)GetStockObject(15);				   // white background brush 
	blahf.lpszMenuName =  NULL;    // name of menu resource 
	blahf.lpszClassName = "cOAMTile";  // name of window class 
	
	
	// Register the window class. 
	RegisterClassEx(&blahf);
	
	
	//Create the Window
	
	
	
hwndTiles= CreateWindowEx(NULL,"cOAMTile",NULL,WS_VISIBLE| WS_CHILD,5,11,256,256,GetDlgItem(me,fraTiles),0,hGlobal,0);





	return 0;
}
LRESULT CALLBACK OAMTileProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
	
	switch (message) 
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		
		OamEditor.Tiles.Blit(hdc,0,0,256,256,0,0);

		EndPaint(hWnd, &ps);
		break;
		
	case WM_LBUTTONDOWN:
	
		break; 
		
		
		
		
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
	
}
int cOAMEdit::CreateSpriteHWND(){

WNDCLASSEX blahf;
	memset(&blahf,0,sizeof(blahf));
	blahf.cbSize = sizeof(blahf);		   // size of structure 
	blahf.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	blahf.lpfnWndProc = &OAMSpriteProc;	  // points to window procedure 
	blahf.cbClsExtra = NULL;				// no extra class memory 
	blahf.cbWndExtra =NULL; 			   // no extra window memory 
	blahf.hInstance = hGlobal;		   // handle to instance 
	blahf.hIcon = NULL; 			 // predefined app. icon 
	blahf.hCursor = NULL;				// predefined arrow 
	blahf.hbrBackground = (HBRUSH)GetStockObject(RGB(255,255,255));				   // white background brush 
	blahf.lpszMenuName =  NULL;    // name of menu resource 
	blahf.lpszClassName = "cOAMSprite";  // name of window class 
	
	
	// Register the window class. 
	RegisterClassEx(&blahf);
	
	
	//Create the Window
	
	
	
	hwndSpritePreview= CreateWindowEx(NULL,"cOAMSprite",NULL,WS_VISIBLE| WS_CHILD,5,11,256,512,GetDlgItem(me,fraSprPreview),0,hGlobal,0);





	return 0;
}
LRESULT CALLBACK OAMSpriteProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
	
	switch (message) 
	{
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		
		
	OamEditor.SGBSpr.PreviewSprite.Blit(hdc,0,0,256,256,0,0);
	OamEditor.DrawSelObj(hdc);
		EndPaint(hWnd, &ps);
		break;
		
	case WM_LBUTTONDOWN:
	
		break; 
		
		
		
		
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
	
}
int cOAMEdit::UpdateSize(){
int i=0;
char  sizes[3][4][7]={
		{ "8, 8", "16,16","32,32", "64,64" }, 
		{ "16,8", "32,8" ,"32,16", "64,32" },
		{ "8,16", "8,32" ,"16,32", "32,64" },
	};

cboSizes.Clear();
for(i = 0; i<4;i++) cboSizes.Additem((char*)sizes[cboShapes.GetListIndex()][i]);

cboSizes.SetListIndex(0);


	return 0;
}


int cOAMEdit::GetSpriteData(){//Fillsout the SprGBuf
char offset[256]={0};

SGBSpr.id=lstSprite.GetListIndex();
sprintf(offset,"%X",SpriteOAM[SGBSpr.id]);
SetWindowText(GetDlgItem(me,txtFOff),offset);
	return 0;
}

int cOAMEdit::GetFrames(){
u32 offset=0; 
int i=0;
unsigned int blah=0;
char wndstr[256]={0};
GetWindowText(GetDlgItem(me,txtFOff),wndstr,8);
sscanf(wndstr,"%X",&blah);
 offset= blah;
 fseek(GBA.ROM,offset,SEEK_SET);
 fread(&FrameTable,2040, 1,GBA.ROM);
	 for(i=0;i<0xFF;i++){
		 if(FrameTable[i].Pointers==0){
			maxframe=i;
			break;
		 }
	 }
//Load the frame combo and call a function to fill all that out
	 cboFrames.Clear();
	 for(i=0; i<maxframe;i++){
		 sprintf(wndstr,"%X",i);
		 cboFrames.Additem(wndstr);
	 }
	 cboFrames.SetListIndex(0);
	
	return 0;


}
int cOAMEdit::SetFrames(){
    char buffer[256]={0};

	sprintf(buffer,"%X",FrameTable[cboFrames.GetListIndex()].Frame);
	SetWindowText(GetDlgItem(me,txtTimer),buffer);
	sprintf(buffer,"%X",FrameTable[cboFrames.GetListIndex()].Pointers);
	SetWindowText(GetDlgItem(me,txtFPOff),buffer);

	return 0;
}

int cOAMEdit::SetParts(){
int hflip=0;
int vflip=0;
int thispart = cboPartNo.GetListIndex();
	const u8 objSizes[3][4][2]=
	{
		{ { 8, 8}, {16,16}, {32,32}, {64,64} }, 
		{ {16, 8}, {32, 8}, {32,16}, {64,32} },
		{ { 8,16}, { 8,32}, {16,32}, {32,64} },
	};
	
//Fill it out
cboPal.SetListIndex(SGBSpr.OAM[thispart].deOAM.Palette);
VChk.value(SGBSpr.OAM[thispart].deOAM.VerticalFlip);
HChk.value(SGBSpr.OAM[thispart].deOAM.HorizontalFlip);
cboShapes.SetListIndex(SGBSpr.OAM[thispart].deOAM.ObjShape);
UpdateSize();
cboSizes.SetListIndex(SGBSpr.OAM[thispart].deOAM.ObjSize);
int    	sx = ( SGBSpr.OAM[thispart].enOAM.OAM1 & 511);
int 	sy = ( SGBSpr.OAM[thispart].enOAM.OAM0 & 255);
		if (sy & 0x80) 
			sy = sy - 256; //check for the negative
		if (sx &  0x100)  
			sx = sx - 511; //same here
         
		sx = sx + 16;
		sy = sy + 16;
		//Set up the basic tile
    
       
	
		cboPal.SetListIndex( (( SGBSpr.OAM[thispart].enOAM.OAM2&0xF000))/0x1000);

		


	    cboShapes.SetListIndex(  SGBSpr.OAM[thispart].enOAM.OAM0>>14);
		UpdateSize();
		cboSizes.SetListIndex( SGBSpr.OAM[thispart].enOAM.OAM1>>14);
       
		   HChk.value(0);
		if(  SGBSpr.OAM[thispart].enOAM.OAM1 & 0x1000) //XFLIP
		   HChk.value(1);
		   
			VChk.value(0);
		if(  SGBSpr.OAM[thispart].enOAM.OAM1 & 0x2000 ) //YFLIP
		   VChk.value(1);
		hflip = ( HChk.value() ? (objSizes[ SGBSpr.OAM[thispart].enOAM.OAM0>>14][ SGBSpr.OAM[thispart].enOAM.OAM1>>14][0])-1:0);
		vflip = ( VChk.value() ? (objSizes[ SGBSpr.OAM[thispart].enOAM.OAM0>>14][ SGBSpr.OAM[thispart].enOAM.OAM1>>14][1])-1:0);
		    rPart.left   = ((sx/8)^(hflip/8))*8;
			rPart.top    = ((sy/8)^(vflip/8))*8;
			rPart.right  = ((rPart.left) +(objSizes[ SGBSpr.OAM[thispart].enOAM.OAM0>>14][ SGBSpr.OAM[thispart].enOAM.OAM1>>14][0]));
			rPart.bottom = ((rPart.top ) + (objSizes[ SGBSpr.OAM[thispart].enOAM.OAM0>>14][ SGBSpr.OAM[thispart].enOAM.OAM1>>14][1]));
      InvalidateRect(hwndSpritePreview,0,1);
							
	
	return 0;
}

int cOAMEdit::SetupPreview(){
long GFXPnt=0;
long PalPnt=0;
long palsize=0;
long addybuf=0;
long size=0;
int i=0;
int ii=0;
char blah[256]={0};
u8 decompbuf[32687]= {0};
SGBSpr.PreviewSprite.Clear();
InvalidateRect(me,0,1);
if(SpriteOAM[SGBSpr.id]==0) return 0;
	memset(&SGBSpr.PreRAM[0x4000], 0, 0x4000);
CompHeader thiscompheader;

	u16 transferpal[256]={0};

	int  X=0;
	long off=0;
	int x =0;
	int frame=cboFrames.GetListIndex();
	memset(SprPal,0,sizeof(SprPal));

	if(crf==0){
		GFXPnt= sprgfxpnt + (SGBSpr.id-0x10) * 4;
		
		fseek(GBA.ROM, sprgfxpnt + (SGBSpr.id -0x10 ) * 4, SEEK_SET);
		fread(&off, 4, 1, GBA.ROM);
		fseek(GBA.ROM, off-0x8000000, SEEK_SET);
		fread(&thiscompheader.check_ten, 1, 1, GBA.ROM);       //Check byte should be 0x10 for lz
		fread(&thiscompheader.l3, 1, 1, GBA.ROM);
		fread(&thiscompheader.l2, 1, 1, GBA.ROM);
		fread(&thiscompheader.l1, 1, 1, GBA.ROM);

		PalPnt = sprpalpnt + (SGBSpr.id-0x10) * 4;
	
		SGBSpr.palsize= (thiscompheader.l2 & 0xF8) * 2;

	
	}else if(crf==1){
        GFXPnt= 0x79A5D8 + (SGBSpr.id-0x10) * 4;
		


		PalPnt= 0x79A8D4 + (SGBSpr.id-0x10) * 4;

		SGBSpr.palsize=  ((MFSprSize[(SGBSpr.id-0x10)<<1]/2048)*16);


	}
	
    
       
		fseek(GBA.ROM, PalPnt , SEEK_SET);
		fread(&addybuf, 4, 1, GBA.ROM);
		fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
		//fread(&transferpal, 1, (paltransfer[x].Size)*2, GBA.ROM);
		memset(GBASprPal,0,0x200);
         fread(&transferpal, 1, SGBSpr.palsize*2, GBA.ROM);
//	if(paltransfer[x][1] == 0) continue;
	 memcpy(&GBASprPal[128], &transferpal, SGBSpr.palsize*2);
           
	 GBA.DecodePal(GBASprPal, SGBSpr.PreviewPal, 16, 0);
  


	
		switch(crf){
				case 0:
					fseek(GBA.ROM, GFXPnt, SEEK_SET);
					fread(&addybuf, 4, 1, GBA.ROM);
					fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
					fread(GBA.CompBuffer, 1, sizeof(GBA.CompBuffer), GBA.ROM);
					SGBSpr.graphicsize = GBA.LZ77UnComp(GBA.CompBuffer, decompbuf);
					memcpy(&SGBSpr.PreRAM[0x4000], &decompbuf, SGBSpr.graphicsize);
				break; 
				case 1:
                    SGBSpr.graphicsize = MFSprSize[(SGBSpr.id-0x10)<<1];
					fseek(GBA.ROM, GFXPnt, SEEK_SET);
					fread(&addybuf, 4, 1, GBA.ROM);
					fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
				    fread(&SGBSpr.PreRAM[0x4000], 1, SGBSpr.graphicsize, GBA.ROM);
				break;
		}
	

		SGBSpr.Tiles.Load(SGBSpr.PreRAM, 1023);
		Tiles.SetPalette(SGBSpr.PreviewPal);
		Tiles.Clear();
			for(i= 512; i<1024;i++){
				Tiles.Draw(SGBSpr.Tiles,((i-512) % 32)*8,((i-512)/32)*8,0x8000+i);
			}
	OAMED=true;
    DecodeSprite(&SGBSpr,FrameTable[frame].Pointers-0x8000000);
	OAMED=false;
	DrawPSprite(&SGBSpr);
	cboPartNo.Clear();
	for(i=0;i<SGBSpr.maxparts;i++){
		sprintf(blah,"%X",i);
		cboPartNo.Additem(blah);
	}
	cboPartNo.SetListIndex(0);
	InvalidateRect(me,0,1);
	return 0;
}
int cOAMEdit::DrawSelObj(HDC hdc){

    RECT myrect;
	HBRUSH curbrush = CreateSolidBrush(RGB(255, 0, 0));

	FrameRect(hdc, &rPart,curbrush);
	DeleteObject(curbrush);
	return 0;
}

int  cOAMEdit::UpdateSprite(){//Routine unpacks structure then repacks it 
//Decode current info
	int thispart = cboPartNo.GetListIndex();
	u16 curoam=0;

	DecodedOAM  deOAM;
    curoam = SGBSpr.OAM[thispart].enOAM.OAM0;
			deOAM.yCoord    = (curoam & 0xFF);
			deOAM.objRot    = GetBitField(curoam, 8, 1);
			deOAM.DSaOD     = GetBitField(curoam, 9, 1);
			deOAM.ObjMode   = GetBitField(curoam, 10, 2);
			deOAM.ObjMosiac = GetBitField(curoam, 12, 1);
			deOAM.Paltype   = GetBitField(curoam, 13, 1);
			deOAM.ObjShape  = GetBitField(curoam, 14, 2);//Just using this
			
			deOAM.ObjShape  = cboShapes.GetListIndex();
			SGBSpr.OAM[thispart].enOAM.OAM0 =0;
			//SGBSpr.OAM[thispart].enOAM.OAM0 |= (deOAM.ObjShape << 14);
            //curoam += (deOAM.ObjShape  << 14);
			//curoam += (deOAM.Paltype   << 13);
            //curoam += (deOAM.ObjMosiac << 12);
			//curoam += (deOAM.ObjMode   << 10);
			///curoam += (deOAM.DSaOD     << 9);
			//curoam += (deOAM.objRot    << 8);
			//curoam +=  deOAM.yCoord;
		    curoam = (
				      ((deOAM.ObjShape  &    2) << 14)|
				      ((deOAM.Paltype   &    1) << 13)|
					  ((deOAM.ObjMosiac &    1) << 12)|
					  ((deOAM.ObjMode   &    2) << 10)|
					  ((deOAM.DSaOD     &    1) << 9) |
					  ((deOAM.objRot    &    1) << 8) |
					   (deOAM.yCoord    & 0xFF)
					   );
			

/*			  Bit   Expl.
			  0-8   X-Coordinate           (0-511)
              When Rotation/Scaling used (Attribute 0, bit 8 set):
              9-13  Rotation/Scaling Parameter Selection (0-31)
			  (Selects one of the 32 Rotation/Scaling Parameters that
			  can be defined in OAM, for details read next chapter.)
			  When Rotation/Scaling not used (Attribute 0, bit 8 cleared):
			  9-11  Not used
			  12    Horizontal Flip      (0=Normal, 1=Mirrored)
			  13    Vertical Flip        (0=Normal, 1=Mirrored)
			  14-15 OBJ Size               (0..3, depends on OBJ Shape, see Attr 0)
					  Size  Square   Horizontal  Vertical
					  0     8x8      16x8        8x16
					  1     16x16    32x8        8x32
					  2     32x32    32x16       16x32
					  3     64x64    64x32       32x64*/

			
			curoam = SGBSpr.OAM[thispart].enOAM.OAM1;
	        if (deOAM.objRot  == 1) {
				deOAM.rotation = GetBitField(curoam, 9, 5);
			} else {
				deOAM.HorizontalFlip = (curoam & 0x400 ? 1:0);
				deOAM.VerticalFlip   = (curoam & 0x800 ? 1:0);
			}
			
			deOAM.ObjSize =cboSizes.GetListIndex();
            
			deOAM.HorizontalFlip =  HChk.value();
            deOAM.VerticalFlip =  VChk.value();
			deOAM.ObjSize = cboSizes.GetListIndex();
            curoam^=0x800;
			curoam^=0x400;
			curoam|=(deOAM.HorizontalFlip<<10);
            curoam|=(deOAM.VerticalFlip<<11);
			curoam|=(deOAM.ObjSize<<14);
			SGBSpr.OAM[thispart].enOAM.OAM1 = curoam;



			//0-9   Character Name          (0-1023=Tile Number)
			//10-11 Priority relative to BG (0-3; 0=Highest)
		    //12-15 Palette Number   (0-15) (Not used in 256 color/1 palette mode)
            curoam = SGBSpr.OAM[thispart].enOAM.OAM2;
			deOAM.TileNumber = curoam & 0x3FF;
			deOAM.priority = GetBitField(curoam, 10, 2);
			deOAM.Palette = (curoam & 0xF000)/0x1000;
            deOAM.Palette = cboPal.GetListIndex();
			curoam|=(deOAM.Palette<<12);
		  	SGBSpr.OAM[thispart].enOAM.OAM2 = curoam;
			DrawPSprite(&SGBSpr);
            InvalidateRect(	hwndSpritePreview,0,1);      
	return 0;
}


int ImportPal(){
	char FileName[512]={0};
	int i =0;
	int howmanyexist=0;
	int maxpals = (MFSprSize[(OamEditor.SGBSpr.id-0x10)<<1]/2048)*16;
	GBA.ReturnFileName("Please select a PAL file to export too\0*.PAL",FileName,512);
	FILE* fp = fopen(FileName,"r+b");
	if(fp){
		if(crf==0){
    
		}else if(crf == 1){
			 maxpals = (MFSprSize[(OamEditor.SGBSpr.id-0x10)<<1]/2048)*16;
		
				
		
		
		}
			for(i=0;i<maxpals;i++)
				fread(&OamEditor.SGBSpr.PreviewPal[128+i],4,1,fp);
			fclose(fp);
			DrawPSprite(&OamEditor.SGBSpr);
			for(i= 512; i<1024;i++){
				  OamEditor.Tiles.Draw(OamEditor.SGBSpr.Tiles,((i-512) % 32)*8,((i-512)/32)*8,0x8000+i);
			}
			InvalidateRect(OamEditor.hwndSpritePreview,0,1);    
			InvalidateRect(OamEditor.hwndTiles,0,1);
			InvalidateRect(OamEditor.hwndPal,0,1);	
	}
return 0; 
}

int ExportPal(){

	char FileName[512]={0};
	int i =0;
	GBA.ReturnFileName("Please select a PAL file to export too\0*.PAL",FileName,512,0);
	FILE* fp = fopen(FileName,"w+b");
	if(fp){
	
	for(i=0;i<OamEditor.SGBSpr.palsize;i++)
		fwrite(&OamEditor.SGBSpr.PreviewPal[128+i],4,1,fp);
	fclose(fp);
	}
//size then palettes
	//FILE* fp  = fopen(
	//OamEditor.SGBSpr.palsize
	InvalidateRect(OamEditor.hwndPal,0,1);
return 0; 
}


int ImportGFX(){

     char FileName[512]={0};
	 u8   Buffer[32192]={0};
	u16 i =0;
	int sz=0;
	GBA.ReturnFileName("Please select a GFX file to export too\0*.GFX",FileName,512,0);
	FILE* fp = fopen(FileName,"r+b");
	if(fp){
		sz=GBA.ReturnEOF(fp);
	
		if((sz>0x4000)&&!(sz>=0x4000))OamEditor.SGBSpr.graphicsize=0x4000;
		//fread(&OamEditor.SGBSpr.PreRAM[0x4000],1,OamEditor.SGBSpr.graphicsize,fp);
		fseek(fp,0,SEEK_SET);
		for(i=0; i<sz;i++) OamEditor.SGBSpr.PreRAM[0x4000+i]=fgetc(fp);
		OamEditor.SGBSpr.Tiles.Load(OamEditor.SGBSpr.PreRAM, 1024);
		
		
		OamEditor.Tiles.SetPalette(OamEditor.SGBSpr.PreviewPal);
		OamEditor.Tiles.Clear();
		for(i= 512; i<1024;i++){
			OamEditor.Tiles.Draw(OamEditor.SGBSpr.Tiles,((i-512) % 32)*8,((i-512)/32)*8,0x8000+i);
		}
		InvalidateRect(OamEditor.hwndTiles,0,1);
	    DrawPSprite(&OamEditor.SGBSpr);
		InvalidateRect(OamEditor.hwndSpritePreview,0,1);   
		//InvalidateRect(OamEditor.hwndTiles,0,1);
		InvalidateRect(OamEditor.me,0,1);
		fclose(fp);
	}
return 0; 
}


int ExportGFX(){
//This doesn't need a size 
     char FileName[512]={0};
	int i =0;
	GBA.ReturnFileName("Please select a GFX file to export too\0*.GFX",FileName,512,0);
	FILE* fp = fopen(FileName,"w+b");
	if(fp){

    fwrite(&OamEditor.SGBSpr.PreRAM[0x4000],OamEditor.SGBSpr.graphicsize,1,fp);
	fclose(fp);
	}
return 0; 
}
int SavePal(){
	u32 offset=0;
 //GFXPnt= 0x79A5D8 + (SGBSpr.id-0x10) * 4;
	        //GFXPnt= 0x79A5D8 + (SGBSpr.id-0x10) * 4;
		


		//PalPnt= 0x79A8D4 + (SGBSpr.id-0x10) * 4;

		//SGBSpr.palsize=  ((MFSprSize[SGBSpr.id-0x10]/2048)*16);


	
	
    
       
		//fseek(GBA.ROM, PalPnt , SEEK_SET);
		//fread(&addybuf, 4, 1, GBA.ROM);
		//fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
		//fread(&transferpal, 1, (paltransfer[x].Size)*2, GBA.ROM);
	    //memset(GBASprPal,0,0x200);
        // fread(&transferpal, 1, SGBSpr.palsize*2, GBA.ROM);
//	if(paltransfer[x][1] == 0) continue;
	 //memcpy(&GBASprPal[128], &transferpal, SGBSpr.palsize*2);
       
	   GBA.EncodePal(GBASprPal, OamEditor.SGBSpr.PreviewPal, 16, 0);
       offset=GBA.FindFreeSpace(OamEditor.SGBSpr.palsize*2,0xFF);
	   fseek(GBA.ROM,offset,SEEK_SET);
	   fwrite(&GBASprPal[128],1,OamEditor.SGBSpr.palsize*2,GBA.ROM);
	   offset+=0x8000000;
	   if(crf ==0){
		fseek(GBA.ROM, sprpalpnt + (OamEditor.SGBSpr.id-0x10) * 4,SEEK_SET);
	   }else if(crf==1){
	    fseek(GBA.ROM,  0x79A8D4 + (OamEditor.SGBSpr.id-0x10) * 4,SEEK_SET);
	   }
	   fwrite(&offset,4,1,GBA.ROM);
	   //SGBSpr.palsize=  ((MFSprSize[SGBSpr.id-0x10]/2048)*16);
	   //MFSprSize[(OamEditor.SGBSpr.id-0x10)<<1]=(OamEditor.SGBSpr.palsize*177);
	  
		GBA.Reopen();
return 0; 
}

int SaveGFX(){ 
	u32 GFXPointer=0;
	u8  GFXbuf[32192]={0};
	u32 size=0;
    if(crf==0){
		//T'will be kind of hard. 
         size=GBA.LZ77Comp(OamEditor.SGBSpr.graphicsize,  &OamEditor.SGBSpr.PreRAM[0x4000], GFXbuf) ;
         //Just find new space fuck the old >_>
         GFXPointer=GBA.FindFreeSpace(size, 0xFF);
		 fseek(GBA.ROM,GFXPointer,SEEK_SET);
		 fwrite(&GFXbuf,1,size,GBA.ROM);
         fseek(GBA.ROM,sprgfxpnt + (OamEditor.SGBSpr.id-0x10) * 4,SEEK_SET);
		 GFXPointer+=0x8000000;
		 fwrite(&GFXPointer,1,4,GBA.ROM);
	}else if(crf==1){
        fseek(GBA.ROM,0x79A5D8 + (OamEditor.SGBSpr.id-0x10) * 4,SEEK_SET);
		fread(&GFXPointer,1,4,GBA.ROM);
		GFXPointer-=0x8000000;
		if(MFSprSize[(OamEditor.SGBSpr.id-0x10)<<1]<OamEditor.SGBSpr.graphicsize || MFSprSize[(OamEditor.SGBSpr.id-0x10)<<1]>OamEditor.SGBSpr.graphicsize  ) {
			MFSprSize[(OamEditor.SGBSpr.id-0x10)<<1]=OamEditor.SGBSpr.graphicsize;
			fseek(GBA.ROM,0x2E4A50,SEEK_SET);
	        fwrite(&MFSprSize,4,0xC0,GBA.ROM);
			GFXPointer=GBA.FindFreeSpace(OamEditor.SGBSpr.graphicsize,0xFF);

		}
        fseek(GBA.ROM,GFXPointer,SEEK_SET);
		
		fwrite(&OamEditor.SGBSpr.PreRAM[0x4000],OamEditor.SGBSpr.graphicsize,1,GBA.ROM);
		GFXPointer+=0x8000000;
		fseek(GBA.ROM,0x79A5D8 + (OamEditor.SGBSpr.id-0x10) * 4,SEEK_SET);
		fwrite(&GFXPointer,1,4,GBA.ROM);
        GBA.Reopen();

	}
 




	return 0;
}


